package com.cilico.utils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


import android.app.Activity;

/**
 * 文件管理
 * 
 * @author lv.xiangan
 *
 */
public final class FileManager {
	/**
	 * @param activity
	 * @param fileName
	 * @param data
	 */
	public static final void updateFile(Activity activity, String fileName, byte[] data) {
		try {
			FileOutputStream fos = activity.openFileOutput(fileName, Activity.MODE_PRIVATE);
			fos.write(new byte[] {}); // clear file.
			fos.write(data);
			fos.flush();
			fos.close();
		} catch (FileNotFoundException e) {
			Utils.printException(e);
		} catch (IOException e) {
			Utils.printException(e);
		} catch (Exception e) {
			Utils.printException(e);
		}

	}

	/**
	 * @param activity
	 * @param fullPathName
	 * @return
	 */
	public static final byte[] readFile(Activity activity, String fullPathName) {
		byte[] buffer = null;
		try {
			FileInputStream fis = activity.openFileInput(fullPathName);
			int len = fis.available();
			buffer = new byte[len];
			fis.read(buffer);
			fis.close();
		} catch (FileNotFoundException e) {
			Utils.printException(e);
		} catch (IOException e) {
			Utils.printException(e);
		} catch (Exception e) {
			Utils.printException(e);
		}
		return buffer;
	}
}
